/*


The MIT License

Copyright (c) 2007 Mohamed El-Shinnawy & Victor Martin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

#import "STSpeakaPreferences.h"

@implementation STSpeakaPreferences


- (id)initWithAppController:(STSpeaka *)appController{
	_appController=appController;
     _prefsCells = [[NSMutableArray alloc] init];

    CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;

    // create the nav bar
    UINavigationBar *navigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)] autorelease];
	UINavigationItem *navigationTitle = [[[UINavigationItem alloc] initWithTitle: @"iSpeaka"] autorelease];
	[navigationBar showButtonsWithLeftTitle:nil rightTitle:nil leftBack:NO];
	[navigationBar setBarStyle:0];
	[navigationBar pushNavigationItem: navigationTitle];
    [[appController speakaView] addSubview:navigationBar];

	/* Initialize the iSpeaka Control */
	/* Show the services on/off switcher, only if the iSpeaka daemon exists */
	CGRect controlPos = CGRectMake(0.0f, 0.0f, 320.0f, 45.0f);
  	//TODO: Check if Daemon exists and it's installed under /Library/LaunchDaemons
  	//NSFileManager *fileManager = [NSFileManager defaultManager];
	//if ([fileManager fileExistsAtPath:@"/Library/LaunchDaemons/com.googlecode.ispeaka.plist"]) {
		ServiceTableCell *cell = [[[SpeakaServiceTableCell alloc] initWithFrame: controlPos andSpeakaPlist:@"/Library/LaunchDaemons/com.googlecode.ispeaka.plist"] autorelease];
		[_prefsCells addObject:cell];
	//}

	//Setup about cells
	UIPreferencesTableCell *aboutCellIcon = [[[UIPreferencesTableCell alloc] init] autorelease];
	[[aboutCellIcon iconImageView] setImage:[[UIImage applicationImageNamed:@"icon.png"] autorelease]];
	[aboutCellIcon setValue:[NSString stringWithFormat:@"iSpeaka v%@",
		[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]];
	[aboutCellIcon setAlignment:2];
	[_prefsCells addObject:aboutCellIcon];
	NSLog(@"settep up about cell");
	UIPreferencesTableCell *aboutCellDescription = [[[UIPreferencesTableCell alloc] init] autorelease];
	[aboutCellDescription setValue: [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleCopyright"]];
	[aboutCellDescription setAlignment:2];
	[_prefsCells addObject:aboutCellDescription];
	NSLog(@"settep up preferences cell");
	//Setup the preferences table
    _prefsTable = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(rect.origin.x, 45.0f, rect.size.width, rect.size.height - 45.0f)];
    [_prefsTable setDataSource:self];
    [_prefsTable setSeparatorStyle:3];
    [_prefsTable reloadData];
    [[appController speakaView] addSubview:_prefsTable];
	NSLog(@"settep up prefstable");
    return self;
}

- (int)numberOfGroupsInPreferencesTable:(id)prefsTable
{
    NSLog(@"in numberOfGroupsInPreferencesTable:");
    return 2;
}

- (int)preferencesTable:(id)prefsTable numberOfRowsInGroup:(int)group
{
    NSLog(@"in preferencesTable:numberOfRowsInGroup:");
    if(group==0) return 1;
    if(group==1) return 2;
}

- (id)preferencesTable:(id)prefsTable cellForRow:(int)row inGroup:(int)group
{
    NSLog([NSString stringWithFormat:@"in preferencesTable:cellForRow:%d inGroup:%d",row,group]);
	if(group==1) row++;
	NSLog(@"drawing row %d",row);
    return [_prefsCells objectAtIndex:row];
	NSLog(@"finished");
}

- (id)preferencesTable:(id)prefsTable titleForGroup:(int)group
{
	if(group==0){
		return @"Preferences";
	}else if(group==1){
		return @"About";
	}
}

- (float)preferencesTable:(id)prefsTable heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposedHeight;
{
    //NSLog(@"in preferencesTable:heightForRow:inGroup:withProposedHeight: and proposed height is %f", proposedHeight);
    //return 48.0f;
    if((group==1) && (row==0)) return proposedHeight*2;
    return proposedHeight;
}


- (void)dealloc
{
    // destroy the ui prefs table
    [_prefsTable release];
    _prefsTable = nil;

    // destroy the cells array
    [_prefsCells release];

    [super dealloc];
}

@end
